﻿Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class tbcontrol
    Inherits TabControl

    Public Sub New()
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)
        Me.ItemSize = New Size(30, 30)
    End Sub

    Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
        MyBase.OnPaintBackground(pevent)
        Dim rct As Rectangle = Me.ClientRectangle
        Dim g As Graphics = pevent.Graphics

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.InterpolationMode = InterpolationMode.HighQualityBicubic

        rct.Width -= 1
        rct.Y += (Me.ItemSize.Height)
        rct.Height -= (Me.ItemSize.Height + 1)

        g.FillRectangle(New SolidBrush(Color.FromKnownColor(KnownColor.ControlLightLight)), rct)

        g.DrawRectangle(Pens.Silver, rct)
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim rct As Rectangle = Me.ClientRectangle
        Dim g As Graphics = e.Graphics

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.InterpolationMode = InterpolationMode.HighQualityBicubic


        For i As Integer = Me.TabCount - 1 To 0 Step -1
            If Me.SelectedIndex <> i Then
                Dim tab_rect As Rectangle = Me.GetTabRect(i)

                g.DrawString(Me.TabPages(i).Text, New Font("Segoe UI", 9), Brushes.DimGray, tab_rect, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
            End If
        Next

        If Me.SelectedIndex <> -1 Then
            Dim tab_rect As Rectangle = Me.GetTabRect(Me.SelectedIndex)

            g.DrawString(Me.TabPages(Me.SelectedIndex).Text, New Font("Segoe UI", 9, FontStyle.Bold), Brushes.Black, tab_rect, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
        End If
    End Sub
End Class
